package com.boot.configstatistical.repair;

import org.json.JSONArray;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Map;

/**
 * @author songlin.an
 * @className Test
 * @description TODO
 * @date 2023/5/18 10:50
 **/
public class OrderRepair {


    public static void main(String[] args) throws IOException {
//        CloseableHttpClient client = HttpClients.createDefault();
//        HttpGet get = new HttpGet("http://man.ledger.dmall.com/ledger/paymentinfo/2/655394246464?includePD=true");
//        BasicHeader header = new BasicHeader("Cookie","dhrUserId=9157561; appname=cloud; username=songlin.an; GALAXY_OFFLINE_CLIENT_WEB_VERSION_9e74bd830361046daa00798aa282f1fe=10; loginType=account; login_user_name=%E5%AE%89%E6%9D%BE%E6%9E%97; login_type=vpn; login_dmall_id=songlin.an; return_url=http%3A//cloud.dmall.com/%23index/dmalltp/home%3AversionType%3DPRO; login_token=Bearer%20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2ODM3NzYxMjc4MDYsImV4cCI6MTY4NDM4MDkyNzgwNiwiaXNzIjoia2VuIiwiZGF0YSI6eyJ1c2VyX2luZm8iOnsidXNlcl9pZCI6OTE1NzU2MSwidXNlcl9jb2RlIjoic29uZ2xpbi5hbiIsInVzZXJfbmFtZSI6Ilx1NWI4OVx1Njc3ZVx1Njc5NyIsInBob25lIjoiIiwiZW1haWwiOiJzb25nbGluLmFuQGRtYWxsLmNvbSIsImhyc19pZCI6ODY0Mzk2MSwiaHJtc19pZCI6MTAwMDEzMDg0MSwidXNlcl9ubyI6IjEwMDAwMTI1OCJ9LCJsb2dpbl90aW1lIjoxNjgzNzc2MTI3ODA2fX0.sc8Rk0_jpF4C_0-m-aCB0e01ctSt6ysTYLcZFae487I; envRootDomain=dmall.com; cur_partner_pc_menu=%5B%7B%22k%22%3A414452%2C%22n%22%3A%22%u6570%u636E%u4E2D%u5FC3%22%2C%22p%22%3A-1%2C%22l%22%3A1%7D%2C%7B%22k%22%3A414482%2C%22n%22%3A%22%u4E1A%u7EE9%22%2C%22p%22%3A414452%2C%22l%22%3A2%7D%2C%7B%22k%22%3A414932%2C%22n%22%3A%22%u6536%u94F6%22%2C%22p%22%3A414482%2C%22l%22%3A3%2C%22r%22%3A-1%7D%2C%7B%22k%22%3A846241%2C%22n%22%3A%22%u652F%u4ED8%u6D41%u6C34%22%2C%22p%22%3A414932%2C%22l%22%3A4%2C%22r%22%3A103901%7D%5D; UYBFEWAEE=EA970CF573B5F3A0725F0C06D37B02DBC3DD18CF3A4AD3A8B5D9EDA5542436333E0994E5D822F51EF3EA220922DFC1C96C32F26F3FAE3F36CB9164804F0369CDA981510C3196C82723F90D29F9E2ED8C; login_no=\"RGQxMDAwMDEyNThfMTU1MzM3ODM5NTc=\"; signature=59e1f80580d3ee996eea73dc7fe6857c; timestamp=1684375597; wm_menu_id=menu_4333");
//        HttpResponse result = client.execute(get);
//        get.setHeader(header);
//        if (result.getStatusLine().getStatusCode() == 200) {
//            String html = EntityUtils.toString(result.getEntity(), "UTF-8");
//            String target = "支付流水号"; // 目标字段
//            Document doc = Jsoup.parse(html);
//            Elements elements = doc.select("td"); // 查找所有td元素
//            for (int i = 0; i < elements.size(); i++) {
//                Element element = elements.get(i);
//                String label = element.previousElementSibling().text(); // 获取前一个兄弟元素的文本，即当前td元素所代表的字段名
//                if (label.contains(target)) { // 如果找到了目标字段
//                    String value = element.text(); // 获取当前td元素的文本，即该字段对应的值
//                    System.out.println(value); // 输出目标值
//                }
//            }
//
//        }

        String inputJson = "{\"cardApportionInfo\": \"[{\\\"brandCard\\\":false,\\\"cardId\\\":\\\"17121\\\",\\\"cardInnerNo\\\":\\\"12200863972116458810000001171098\\\",\\\"cardOuterNo\\\":\\\"8810000001171098\\\",\\\"cardWareApportionList\\\":[{\\\"fee\\\":4980,\\\"promotionalFee\\\":0,\\\"skuId\\\":1130738921,\\\"wareUuid\\\":\\\"1130738921a451a318-d4a4-49ff-afe9-41d835f72690\\\"},{\\\"fee\\\":2816,\\\"promotionalFee\\\":0,\\\"skuId\\\":1080718041,\\\"wareUuid\\\":\\\"1080718041885b1798-83da-4084-a100-66d8e7c3cea3\\\"},{\\\"fee\\\":6363,\\\"promotionalFee\\\":0,\\\"skuId\\\":1132334281,\\\"wareUuid\\\":\\\"1132334281c051ffe5-3474-47fe-8262-06cae040ad1c\\\"},{\\\"fee\\\":5225,\\\"promotionalFee\\\":0,\\\"skuId\\\":1135638761,\\\"wareUuid\\\":\\\"113563876129e083bd-2e75-40b4-b5ea-e17f2fc4e5dc\\\"},{\\\"fee\\\":3980,\\\"promotionalFee\\\":0,\\\"skuId\\\":1070912641,\\\"wareUuid\\\":\\\"1070912641d32d862a-02b3-449f-8357-b574d31bb0d4\\\"},{\\\"fee\\\":7678,\\\"promotionalFee\\\":0,\\\"skuId\\\":1132334281,\\\"wareUuid\\\":\\\"11323342819ec8b786-365a-463d-aa62-c620457cae03\\\"},{\\\"fee\\\":90,\\\"promotionalFee\\\":0,\\\"skuId\\\":1091021441,\\\"wareUuid\\\":\\\"10910214413bbb16ec-a90b-4f7a-9cef-8745c5cc920a\\\"},{\\\"fee\\\":7385,\\\"promotionalFee\\\":0,\\\"skuId\\\":1132334281,\\\"wareUuid\\\":\\\"1132334281efe1c8f6-1618-4d22-b80d-9f4543fd80f2\\\"}],\\\"channelId\\\":\\\"999128401\\\",\\\"deductPrice\\\":38517,\\\"discountPrice\\\":0,\\\"hasApportionBeforPay\\\":1,\\\"initBalance\\\":60000,\\\"limitType\\\":1,\\\"proAmount\\\":3081,\\\"proSubject\\\":[{\\\"fee\\\":3081,\\\"subjectCode\\\":2,\\\"subjectType\\\":1}]},{\\\"brandCard\\\":false,\\\"cardId\\\":\\\"17001\\\",\\\"cardInnerNo\\\":\\\"12200861910187938810000000952771\\\",\\\"cardOuterNo\\\":\\\"8810000000952771\\\",\\\"cardWareApportionList\\\":[{\\\"fee\\\":1399,\\\"promotionalFee\\\":0,\\\"skuId\\\":1132334281,\\\"wareUuid\\\":\\\"1132334281c051ffe5-3474-47fe-8262-06cae040ad1c\\\"},{\\\"fee\\\":11980,\\\"promotionalFee\\\":0,\\\"skuId\\\":1084154841,\\\"wareUuid\\\":\\\"1084154841b9e5a029-17e9-47e1-a504-b8a5a6c39bf7\\\"}],\\\"channelId\\\":\\\"999128401\\\",\\\"deductPrice\\\":13379,\\\"discountPrice\\\":0,\\\"hasApportionBeforPay\\\":1,\\\"initBalance\\\":13379,\\\"limitType\\\":1,\\\"proAmount\\\":1070,\\\"proSubject\\\":[{\\\"fee\\\":1070,\\\"subjectCode\\\":2,\\\"subjectType\\\":1}]}]\"}";  // Replace with your actual JSON string
         inputJson = "{\"cardApportionInfo\": [{\"brandCard\":false,\"cardId\":\"17121\",\"cardInnerNo\":\"12200863972116458810000001171098\",\"cardOuterNo\":\"8810000001171098\",\"cardWareApportionList\":[{\"fee\":4980,\"promotionalFee\":0,\"skuId\":1130738921,\"wareUuid\":\"1130738921a451a318-d4a4-49ff-afe9-41d835f72690\"},{\"fee\":2816,\"promotionalFee\":0,\"skuId\":1080718041,\"wareUuid\":\"1080718041885b1798-83da-4084-a100-66d8e7c3cea3\"},{\"fee\":6363,\"promotionalFee\":0,\"skuId\":1132334281,\"wareUuid\":\"1132334281c051ffe5-3474-47fe-8262-06cae040ad1c\"},{\"fee\":5225,\"promotionalFee\":0,\"skuId\":1135638761,\"wareUuid\":\"113563876129e083bd-2e75-40b4-b5ea-e17f2fc4e5dc\"},{\"fee\":3980,\"promotionalFee\":0,\"skuId\":1070912641,\"wareUuid\":\"1070912641d32d862a-02b3-449f-8357-b574d31bb0d4\"},{\"fee\":7678,\"promotionalFee\":0,\"skuId\":1132334281,\"wareUuid\":\"11323342819ec8b786-365a-463d-aa62-c620457cae03\"},{\"fee\":90,\"promotionalFee\":0,\"skuId\":1091021441,\"wareUuid\":\"10910214413bbb16ec-a90b-4f7a-9cef-8745c5cc920a\"},{\"fee\":7385,\"promotionalFee\":0,\"skuId\":1132334281,\"wareUuid\":\"1132334281efe1c8f6-1618-4d22-b80d-9f4543fd80f2\"}],\"channelId\":\"999128401\",\"deductPrice\":38517,\"discountPrice\":0,\"hasApportionBeforPay\":1,\"initBalance\":60000,\"limitType\":1,\"proAmount\":3081,\"proSubject\":[{\"fee\":3081,\"subjectCode\":2,\"subjectType\":1}]},{\"brandCard\":false,\"cardId\":\"17001\",\"cardInnerNo\":\"12200861910187938810000000952771\",\"cardOuterNo\":\"8810000000952771\",\"cardWareApportionList\":[{\"fee\":1399,\"promotionalFee\":0,\"skuId\":1132334281,\"wareUuid\":\"1132334281c051ffe5-3474-47fe-8262-06cae040ad1c\"},{\"fee\":11980,\"promotionalFee\":0,\"skuId\":1084154841,\"wareUuid\":\"1084154841b9e5a029-17e9-47e1-a504-b8a5a6c39bf7\"}],\"channelId\":\"999128401\",\"deductPrice\":13379,\"discountPrice\":0,\"hasApportionBeforPay\":1,\"initBalance\":13379,\"limitType\":1,\"proAmount\":1070,\"proSubject\":[{\"fee\":1070,\"subjectCode\":2,\"subjectType\":1}]}]}";
        // Define your skuId to barCode mapping

        // Define the keys for skuId and barCode
        String skuIdKey = "skuId";
        String barCodeKey = "barCode";

        String outputJson = addBarCodes(inputJson, skuIdKey, barCodeKey, generateSkuIdToBarCodeMap());
        System.out.println(outputJson);

    }

    public static Map<Integer, Long> generateSkuIdToBarCodeMap() {
        Map<Integer, Long> skuIdToBarCodeMap = new HashMap<>();

        skuIdToBarCodeMap.put(1091021441, 6945431000127L);
        skuIdToBarCodeMap.put(1084154841, 6970338970570L);
        skuIdToBarCodeMap.put(1132334281, 2836875011005L);
        skuIdToBarCodeMap.put(1135638761, 2833097012506L);
        skuIdToBarCodeMap.put(1080718041, 2894354009455L);
        skuIdToBarCodeMap.put(1130738921, 6954840400053L);
        skuIdToBarCodeMap.put(1070912641, 6970618570193L);

        return skuIdToBarCodeMap;
    }



    public static String addBarCodes(String jsonString, String skuIdKey, String barCodeKey, Map<Integer, Long> skuIdToBarCodeMap) {
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONArray cardApportionInfoArray = jsonObject.getJSONArray("cardApportionInfo");

        for (int i = 0; i < cardApportionInfoArray.length(); i++) {
            JSONObject cardInfo = cardApportionInfoArray.getJSONObject(i);
            JSONArray cardWareApportionList = cardInfo.getJSONArray("cardWareApportionList");

            for (int j = 0; j < cardWareApportionList.length(); j++) {
                JSONObject wareApportion = cardWareApportionList.getJSONObject(j);
                int skuId = wareApportion.getInt(skuIdKey);

                // Matching skuId with barCode
                if (skuIdToBarCodeMap.containsKey(skuId)) {
                    long barCode = skuIdToBarCodeMap.get(skuId);

                    // Adding barCode to the JSON
                    wareApportion.put(barCodeKey, barCode);
                }
            }
        }
        return jsonObject.toString();
    }

}
